//
//  HDepartViewController.m
//  NursingManage
//
//  Created by Zhen Huang on 17/5/17.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HDepartViewController.h"
#import "JYJConst.h"
#import "JYJTableCell.h"
#import "Model_Hr_Atd_Scd_Group.h"
#import "HDepartJDViewController.h"
#import "InitDbTable.h"
#import "Model_Hr_Scheduling_Dept.h"
#import "TabelJurisdiction.h"
@interface HDepartViewController ()
<UITableViewDelegate,
UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSArray *widthArray;
@property (nonatomic, copy) NSString *week_begin;
@property (nonatomic, copy) NSString *week_end;
@property (nonatomic, strong) NSMutableDictionary *bigDictionary;
//
@property (nonatomic, strong)NSArray *weekArray;
@end
//
@implementation HDepartViewController
- (instancetype)init
{
    if (self) {
        self.dataArray = [NSMutableArray new];
    }
    return self;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight-NavBarHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UIView alloc]init];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getRelodData];
    [self getWeekTime];
    
    
    
    [self.view addSubview:self.tableView];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

}
- (void)getRelodData
{
    NSArray *array = [[[TabelJurisdiction alloc]init]Jurisdiction];
    self.dataArray = (NSMutableArray *)array;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
// 获取当前周的周一和周日的时间
- (NSString *)getWeekTime
{
    NSDate *nowDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];
    // 获取今天是周几
    NSInteger weekDay = [comp weekday];
    // 获取几天是几号
    NSInteger day = [comp day];
    NSLog(@"%ld----%ld",(long)weekDay,(long)day);
    
    // 计算当前日期和本周的星期一和星期天相差天数
    long firstDiff,lastDiff;
    //    weekDay = 1;
    if (weekDay == 1)
    {
        firstDiff = -6;
        lastDiff = 0;
    }
    else
    {
        firstDiff = [calendar firstWeekday] - weekDay + 1 ;
        lastDiff = 8 - weekDay;
    }
    NSLog(@"firstDiff: %ld   lastDiff: %ld",firstDiff,lastDiff);
    
    // 在当前日期(去掉时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:nowDate];
    [firstDayComp setDay:day + firstDiff];
    NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
    
    NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay   fromDate:nowDate];
    [lastDayComp setDay:day + lastDiff];
    NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];
    NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];
    NSLog(@"%@=======%@",firstDay,lastDay);
    
  InitDbTable *inu =  [[InitDbTable alloc]init];
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@",firstDay,lastDay];
    self.weekArray = [inu getWeekWithTime:firstDay];
    self.week_begin = firstDay;
    self.week_end = lastDay;
    return dateStr;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Model_Hr_Atd_Scd_Group *model = self.dataArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = model.GROUP_NAME;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Model_Hr_Atd_Scd_Group *model = self.dataArray[indexPath.row];
    
    InitDbTable *manager = [[InitDbTable alloc]init];
    
    NSMutableArray *emp_nameArray = [NSMutableArray new];
    
    NSMutableArray *array = [NSMutableArray new];
    
    array = [manager DepartmentModel:model.GROUP_ID :self.week_begin :self.week_end];
    
    for (Model_Hr_Scheduling_Dept *model in array) {
        
        [emp_nameArray addObject:model.EMP_NAME];
    }
    NSMutableArray *arraySCM = [NSMutableArray new];
    
    arraySCM = [manager allModelSCDWithdate:model.GROUP_ID SCD_date:self.week_begin SCD_EndDATE:self.week_end];
    
//    NSMutableArray *dateMutablearray = [@[] mutableCopy];
    
    NSMutableArray *sameArray = [NSMutableArray arrayWithArray:arraySCM];
    
//    NSMutableArray *subArray =[NSMutableArray new];
    
    
    
    self.bigDictionary = [NSMutableDictionary new];
    
    for (int i = 0; i < sameArray.count; i++) {
        
        Model_Hr_Scheduling *schModel = sameArray[i];
        
        NSString *keySting = F(@"%@%@", schModel.SCD_DATE,schModel.EMP_ID);
        
        [self.bigDictionary setObject:schModel.CLASS_NAME forKey:keySting];
    }
    
    
    
    for (int i = 0; i < array.count; i ++) {
        
        NSMutableArray *SubArray = [NSMutableArray new];
        
        Model_Hr_Scheduling_Dept *model = array[i];
    
        for (int j = 0; j < self.weekArray.count; j ++) {
         
            NSString *key = F(@"%@%@",self.weekArray[j],model.EMP_ID);
            
            [SubArray addObject:self.bigDictionary[key]?:@""];
        }
        model.subArray = SubArray;
        
        [array replaceObjectAtIndex:i withObject:model];
    }
    
    NSMutableArray *bigArray = [NSMutableArray new];
    
    for (int i = 0; i < array.count; i ++) {
        
        Model_Hr_Scheduling_Dept *model = array[i];
        
        [bigArray addObject:model.subArray];
    }
    
    array = [manager DepartmentModel:model.GROUP_ID :self.week_begin :self.week_end];
    HDepartJDViewController *HDepartJDVC = [[HDepartJDViewController alloc]initWithGroupID:model.GROUP_ID PeopleArray:bigArray dateArray:emp_nameArray week_begin:self.week_begin week_end:self.week_end ];
    HDepartJDVC.title = model.GROUP_NAME;
    [self.navigationController pushViewController:HDepartJDVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
